const app=getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
      categoriesdata:[],
      endwisedata:[],
      startshow:false,
      start:new Date().getTime()-60*60*7*30*1000,
      end:new Date().getTime(),
      income:0,
      assistmode:0,
      tempFile:0
  },

  /**
   * 组件的方法列表
   */
  methods: {
    bindStartDateChange(e) {
      var t = e.detail.value;
      var T = new Date(t);
      this.data.start=T.getTime();
      this.getLineData();
    },
    bindEndDateChange(e) {
      var t = e.detail.value;
      var T = new Date(t);
      this.data.end=T.getTime();
      this.getLineData();
    },
    toAssistMode(){
      let that=this;
      let assist=app.functions.getLocalStorage('assistmode');
      if(app.functions.isEmpty(assist)){
        app.functions.setLocalStorage('assistmode',1);
        that.setData({
          assistmode:1
        })
        wx.showModal({
          title: '提示', //提示的标题,
          content: '开启代客下单模式', //提示的内容,
          showCancel: true, //是否显示取消按钮,
          cancelText: '取消', //取消按钮的文字，默认为取消，最多 4 个字符,
          cancelColor: '#000000', //取消按钮的文字颜色,
          confirmText: '去购物', //确定按钮的文字，默认为取消，最多 4 个字符,
          confirmColor: '#3CC51F', //确定按钮的文字颜色,
          success: res => {
            if(res.confirm){
              wx.switchTab({
                url: '/pages/home/home'
              })
            }
          }
        });
      }else{
        app.functions.removeLocalStorage('assistmode');
        that.setData({
          assistmode:0
        })
      }
    },
    getData() {
      this.GetIncomeExpect();
      // this.getLineData();
      // this.getMothElectro();

    },
    
    getLineData(){
      let that=this;
      let userInfo = app.functions.getLocalStorage("user");
      let params={
        memberId:userInfo.id,
        start:that.start,
        end:that.end
      };
       api.orderCount({
          data:params,
          hideLoading:false,
          token:true,
          contenttype:false,
          success(res){
            let catedata=[];
            let enddata=[];
            if(res.data.data.length>0){
              res.data.data.forEach(function(item,index,array){
                enddata.push(item.orderNum);
                catedata.push(item.orderDate.substring(5));
              })
              that.setData({
                categoriesdata:catedata,
                endwisedata:enddata
              },function(){
                that.getMothElectro();
              })
              
              
            }else{
              that.setData({
                categoriesdata:[],
                endwisedata:[]
              })
            }
          },
          fail(){}
        })
    },
    getMothElectro(){
      let that=this;
      var windowWidth = 350;
      try {
        var res = wx.getSystemInfoSync();
        windowWidth = res.windowWidth;
      } catch (e) {
        console.error('getSystemInfoSync failed!');
      }
      let yuelineChart = new wxCharts({ //当月用电折线图配置
        canvasId: 'yueEle',
        type: 'line',
        categories: that.categoriesdata, //categories X轴
        animation: true,
        background: '#ffffff',
        series: [{
          name: '商品销售数量',
          //data: yuesimulationData.data,
          data: that.endwisedata,
          format: function (val, name) {
            return val.toFixed(2) + 'kWh';
          }
        }],
        xAxis: {
          disableGrid: true
        },
        width: windowWidth,
        height: 200,
        dataLabel: false,
        dataPointShape: true,
        extra: {
          lineStyle: 'curve'
        }
      });
      setTimeout(()=>{
        wx.canvasToTempFilePath({
          x: 0,
          y: 0,
          width: 375,
          height: 200,
          destWidth: 750,
          destHeight: 400,
          canvasId: 'yueEle',
          success(res) {
            that.tempFile=res.tempFilePath;
          }
        })
      },1000)
      
      
    },

    // 今日预计收益
    GetIncomeExpect(){
      let that=this;
      let userInfo = app.functions.getLocalStorage("user");
      let params={
        memberId:userInfo.id
      };
      app.api.incomeExpect(params, (res) => {
        that.setData({
          income: res.data
        })
      })
    
       
    },
  }
})
